package com.demo2do.fms.queue.handler.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.demo2do.core.utils.JsonUtils;
import com.demo2do.fms.entity.vehicle.tractor.Tractor;
import com.demo2do.fms.entity.vehicle.trailer.Trailer;
import com.demo2do.fms.queue.entity.VehicleStatusStat;
import com.demo2do.fms.queue.handler.VamHandler;

/**
 *
 * @author Ark
 *
 */
@Component("vamHandler")
public class VamHandlerImpl implements VamHandler {

    private static final Logger logger = LoggerFactory.getLogger(VamHandlerImpl.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.queue.handler.VamHandler#syncVehicleStatus(java.lang.String, com.demo2do.fms.entity.vehicle.Tractor, com.demo2do.fms.entity.vehicle.Trailer)
     */
    public void syncVehicleStatus(String type, Tractor tractor, Trailer trailer) {
        
        try {
            
            // 构建车辆审核状态信息
            VehicleStatusStat stat = null;
            if (StringUtils.equals("tractor", type)) {
                stat = new VehicleStatusStat(tractor);
            } else {
                stat = new VehicleStatusStat(trailer);
            }
            
            // 发送信息到消息队列
            String jsonString = JsonUtils.toJson(stat);
            byte[] messageBody = jsonString.getBytes("UTF-8");

            Message message = MessageBuilder.withBody(messageBody)
                                            .setContentType(MessageProperties.CONTENT_TYPE_JSON)
                                            .build();
            
            rabbitTemplate.setQueue("sync-vehicle-status");
            rabbitTemplate.convertAndSend("direct-fms", "sync-vehicle-status", message);
            
        } catch (Exception e) {
            logger.error("#syncVehicleStatus() - occur exception=[{}]", e);
        }
        
    }
    
}
